import Vue from 'vue';
import Vuex from 'vuex';
import VueAxios from 'vue-axios';
import Axios from 'axios';

Vue.use(Vuex);
Vue.use(VueAxios, Axios);
export const store = new Vuex.Store({
    state: {
      count: 0,
      curMode : "首页",
      data_news:[],
      data_products:[],
      data_curProduct:{}, //当前商品的详情(不包括评论与评论数)
      data_curComments:[], //当前商品的所有评论（由当前商品的_id决定）
    },
    getters:{
        myCount(state){
            return state.count;
        },
        myCurMode(state){
            return state.curMode
        },
        myData_news(state){
          return state.data_news
        },
        myData_products(state){
          return state.data_products
        },
        myData_curProduct(state){
          return state.data_curProduct
        },
        myData_curComments(state){
          return state.data_curComments
        }
    },
    mutations: {
      increment (state) {
        state.count++
      },
      changeCurMode(state,payload){
        state.curMode = payload;
        // console.log(state.curMode);
      },
      //请求所有新闻
      mut_reqNews(state){
        Axios.post("http://localhost:3000/news/list",{limit:20})
        .then(data=>{
            state.data_news=data.data.rows;
        })
      },
      //请求所有商品
      mut_reqProducts(state){
        Axios.post("http://localhost:3000/goods/list",{limit:20})
        .then(data=>{
            state.data_products=data.data.rows;
            // console.log(state.data_products); 
        })
      },
      //请求当前商品详情以及该商品的评论
      mut_reqDetailProduct(state,payload){
        Axios.get("http://localhost:3000/goods/data/"+payload)
        .then(data=>{
            state.data_curProduct=data.data;
            console.log(state.data_curProduct);
        })
      },
      mut_reqDetailComment(state,payload){
        Axios.post("http://localhost:3000/comment/list",{
          limit:30,
          belongid:payload
        })
        .then(data=>{
            state.data_curComments=data.data.rows;
            console.log(state.data_curComments);
        })
      },
    },
    actions:{
      reqNews ({ commit }) {
        commit('mut_reqNews')
      },
      reqProducts ({ commit }) {
        commit('mut_reqProducts')
      },
      reqDetailProduct ({ commit },payload) {
        commit('mut_reqDetailProduct',payload)
      },
      reqDetailComment ({ commit },payload) {
        commit('mut_reqDetailComment',payload)
      }
      
    }
  })
